/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.geometricconstraint.ObjectPlacement;
import org.openaecfm.model.kernel.Object;
import org.openaecfm.model.kernel.Product;
import org.openaecfm.model.representation.ProductRepresentation;
import org.openaecfm.service.kernel.IProductManager;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class ProductIO implements IEntityInstanceUtilInput<Product>,
		IEntityInstanceUtilOutput<Product> {
	private IEntityInstanceUtilInput<Object> utilInput;
	private IEntityInstanceUtilOutput<Object> utilOutput;
	private IProductManager manager;

	public void setManager(IProductManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(IEntityInstanceUtilOutput<Object> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(Product pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set ObjectPlacement
		if (pojo.getObjectPlacement() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getObjectPlacement())));
		}

		// Set Representation
		if (pojo.getRepresentation() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getRepresentation())));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, Product pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Product pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ObjectPlacement
		Parameter op = sei.getParameter(5);
		if (op.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) op;
			manager.setObjectPlacement(pojo,(ObjectPlacement) appDataManager
					.getObject(n.toInstanceName()));
		}

		// Set Representation
		Parameter rep = sei.getParameter(6);
		if (rep.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) rep;
			manager.setRepresentation(pojo,(ProductRepresentation) appDataManager
					.getObject(n.toInstanceName()));
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Object> utilInput) {
		this.utilInput = utilInput;
	}
}
